<script setup lang="ts">
import dayjs from 'dayjs'
import { getNewsDetail, getNewsPage } from '@/api/news'
const show = ref(false)
const page = ref(1)
const route = useRoute()
const router = useRouter()
const newsData = ref<any[]>([])
const newsInfo = ref<any>({})
const newsCount = ref<any>(0)
const id = ref<any>(route.query.id)
watch(() => route.query, (query) => {
  if (query.id) {
    id.value = +query.id
    handleGetNewsDetil(id.value)
  }
  else { id.value = null }

  if (query.id)
    id.value = query.id
}, { immediate: true })

handleGetNewsPage()
// 获取新闻列表分页
async function handleGetNewsPage() {
  const result: any = await getNewsPage({
    size: 6,
    page: page.value,
    order: 'index',
    sort: 'asc',
  })
  show.value = false
  newsData.value = newsData.value.concat(result.list)
  await nextTick()
  show.value = true
  newsCount.value = result.pagination.total
}

// 获取新闻详情
async function handleGetNewsDetil(id: any) {
  const result: any = await getNewsDetail({ id })
  newsInfo.value = result
}

function handleLoadMore() {
  page.value++
  handleGetNewsPage()
}

const formatDate = computed(() => {
  return (val: any) => {
    return dayjs(val).format('YYYY-MM-DD')
  }
})

function onClick(item: any) {
  router.push(`/newsDetail?id=${item.id}`)
}

const el = ref(null)

useIntersectionObserver(el, ([{ isIntersecting }]) => {
  if (isIntersecting)
    show.value = isIntersecting
})

function onEnter(el: any, done: any) {
  el.style.opacity = 0
  const delay = el.dataset.index * 200
  const animation = 'fadeInRight 0.8s infinite'
  setTimeout(() => {
    el.style.transition = 'opacity 1s'
    el.style.opacity = 1
    el.style.animation = animation
    el.style['animation-iteration-count'] = 1
    done()
  }, delay)
}
</script>

<template>
  <div class="news">
    <img class="banner" src="https://public-oss-file.zmaxfilm.com/website-compress/image-29.png" alt="公司资讯">
    <div v-if="id" class="breadcrumb">
      <div class="content">
        <span class="text-666 text-[16px]">公司资讯</span>
        <UnoIcon class="i-ic-round-arrow-forward-ios text-[14px] mx-[24px] text-999" />
        <span class="text-666 text-[16px]">资讯详情</span>
      </div>
    </div>
    <div v-if="id" key="info" class="news-info">
      <span class="text-[28px] font-bold leading-none">{{ newsInfo.title }}</span>
      <span class="text-[18px] font-medium text-999 mt-[50px] leading-none date">发表时间：{{ formatDate(newsInfo.releaseDate)
      }}</span>
      <div class="news-content" v-html="newsInfo.content" />
    </div>
    <div v-else ref="el" key="list" class="list">
      <transition-group @enter="onEnter">
        <div v-for="(item, index) in newsData" v-show="show" :key="index" :data-index="index" class="news-item"
          @click="onClick(item)">
          <img class="news-img" :src="item.cover" alt="图片">
          <div class="news-content">
            <span class="news-title">{{ item.title }}</span>
            <div class="flex items-center">
              <span class="date-text">{{ formatDate(item.releaseDate) }}</span>
            </div>
          </div>
        </div>
      </transition-group>
    </div>
    <div v-if="newsCount > page * 6 && !id" class="more-button" @click="handleLoadMore">
      <span class="text-[22px] font-medium text-666">加载更多</span>
      <UnoIcon class="i-ic-outline-keyboard-arrow-down text-[26px] font-bold ml-[12px] text-theme" />
    </div>
  </div>
</template>

<style lang="scss">
.news {
  .news-info {
    .news-content {
      p {
        margin: 20px 0;
        font-size: 18px;
      }

      img {
        width: 100%;
        margin: 5px 0;
        object-fit: cover;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
.news {
  display: flex;
  min-height: 500px;
  flex-direction: column;

  .banner {
    width: 100%;
    height: 600px;
  }

  .breadcrumb {
    height: 56px;
    background-color: #FAFAFA;

    .content {
      height: 100%;
      width: 1200px;
      margin: 0 auto;
      display: flex;
      align-items: center;
    }
  }

  .news-info {
    display: flex;
    flex-direction: column;
    width: 1200px;
    margin: 0 auto;
    display: flex;
    padding-top: 50px;
    padding-bottom: 80px;

    .date {
      padding-bottom: 30px;
      border-bottom: 1px solid #EEEEEE;
    }

    .news-content {
      font-size: 18px;
      font-weight: 500;
      line-height: 36px;
      margin-top: 44px;
    }
  }

  .list {
    width: 1200px;
    margin: 0 auto;
    padding: 80px 0;
    display: grid;
    grid-gap: 30px;
    grid-template-columns: repeat(3, 1fr);

    .news-item {
      flex: 1;
      height: 350px;
      display: flex;
      cursor: pointer;
      flex-shrink: 0;
      flex-direction: column;

      &:hover {
        .news-title {
          color: var(--theme-color);
        }
      }

      .news-img {
        width: 100%;
        height: 200px;
        flex-shrink: 0;
        object-fit: cover;
      }

      .news-content {
        height: calc(100% - 200px);
        display: flex;
        padding: 18px 20px;
        flex-direction: column;
        box-sizing: border-box;
        justify-content: space-between;
        background-color: #FAFAFA;

        .news-title {
          font-size: 26px;
          font-weight: bold;
          margin-bottom: 10px;
          transition: all 0.2s ease;
          display: inline-block;
          transition: all 0.2s ease;
          text-overflow: -o-ellipsis-lastline;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          line-clamp: 2;
          -webkit-box-orient: vertical;
        }

        .date-text {
          font-size: 16px;
          font-weight: 500;
          color: #666666;
        }
      }
    }
  }

  .more-button {
    cursor: pointer;
    display: flex;
    margin: 0 auto;
    margin-bottom: 100px;
    align-items: center;
    justify-content: center;
  }
}
</style>
